//
//  CGameSoundPlayW.m
//  3GuoTD
//
//  Created by waver on 10-8-13.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "CGameSoundPlayW.h"

static CGameSoundPlayW * __self = nil;
static	CDSoundEngine * __pSoundEngine = nil;
@implementation CGameSoundPlayW



+(id)GameSoundPlayShare
{
	if(__self==nil)
	{
		__self = [[CGameSoundPlayW alloc] initSound];
	}
	return __self;
}

-(id)initSound
{
	__pSoundEngine = [[CDSoundEngine alloc] init:kAudioSessionCategory_AmbientSound];
	
	int channelGroups[(int)S_MAX];
	for(int i=0;i<(int)S_MAX;i++)
	{
		channelGroups[i] = 1;
		__pitch[i] = 1.0;
		__gain[i] = 1.0;
		__pan[i] = 0.5;
	}
	
	__pitch[0]= 1.0;
	
	[__pSoundEngine defineSourceGroups:channelGroups total:(int)S_MAX];
	
	
	[self LoadSound];
	
	return self;
}

-(void)LoadSound
{
	NSArray * nameArray = [NSArray arrayWithObjects:
						   @"BGM lvl02-0816.wav",
						   @"two.wav",
						   @"three.wav",
						   @"one.wav",
						   @"SFX03-sword0813.wav",
						   @"SFX04-loop0816.wav",
						   @"SFX05-loop0816.wav",
						   @"SFX06-fight0816.wav",
						   @"two.wav",
						   @"three.wav",
						   nil];
	
	for(int i=0; i< [nameArray count];i++)
	{
		[__pSoundEngine loadBuffer:i filePath:[nameArray objectAtIndex:i]];
	}
}

-(void)PlaySound:(int)soundId
{
	int m_id = (int)soundId;
	[__pSoundEngine playSound:m_id sourceGroupId:m_id 
						pitch:__pitch[m_id] pan:__pan[m_id] gain:__gain[m_id] loop:YES];
}

-(void)SetSoundVolume:(int)soundId SoundVolume:(float)volume
{
	
	int m_id = (int)soundId;
	if(__pitch[m_id] != volume)
	{
		__pitch[m_id] = volume;
		[__pSoundEngine ChangeVolume:m_id sourceGroupId:m_id pitch:volume];
	}
}


-(void)SetSoundMut:(int)soundId Mut:(BOOL)isMut;
{
	[[[__pSoundEngine soundSourceForSound:soundId sourceGroupId:soundId] retain] setMute:isMut];
	[[[__pSoundEngine soundSourceForSound:soundId sourceGroupId:soundId] retain] setLooping:YES];
	
}

-(void)SetSoundEnable:(int)soundId SoundAble:(BOOL)isAble
{
	[__pSoundEngine setSourceGroupEnabled:soundId enabled:isAble];
}

@end
